package com.ruijie.autotest.log.validation.paibo.tcp8848;

import com.minelx.util.socket.TCPUtil;
import com.ruijie.autotest.log.common.crypto.Crypto;
import com.ruijie.autotest.log.common.socket.TCPPortTableFactory;
import com.ruijie.autotest.log.core.data.ITable;
import com.ruijie.autotest.log.impl.data.Columns;
import com.ruijie.autotest.log.impl.data.SimpleTable;
import com.ruijie.autotest.log.impl.parse.SplitCellsParser;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.Socket;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TCPPortTableFactoryTest {
	@Test
	void createTable() throws IOException, InterruptedException {
		SimpleTable.Builder tableBuilder = new SimpleTable.Builder(new Columns(new String[]{"firstColumn", "secondColumn"}));

		TCPPortTableFactory factory = new TCPPortTableFactory(
				8890,
				tableBuilder,
				new SplitCellsParser("\u0001"),
				Crypto.doNothing());
		factory.start();

		try (Socket socket = new Socket("localhost", 8890)) {
			TCPUtil.oneshot(socket, "firstColumnValue\u0001secondColumnValue");
		}
		Thread.sleep(100);
		ITable table = factory.createTable();
		factory.stop();
		assertEquals("firstColumnValue", table.getRow(0).stringValueOf("firstColumn"));
		assertEquals("secondColumnValue", table.getRow(0).stringValueOf("secondColumn"));
	}
}
